﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.ComponentModel;

namespace CustomControls
{
    public class FunnelControl : Control
    {
        #region 私有字段
        private System.Windows.Forms.Timer animationTimer;
        private float waterLevel = 70; // 水位百分比 (0-100)
        private float flowRate = 2.0f; // 水流速度
        private float animationOffset = 0; // 动画偏移量
        private Color funnelColor = Color.SlateGray; // 漏斗颜色
        private Color waterColor = Color.FromArgb(150, 66, 165, 245); // 水颜色
        private Color pipeColor = Color.SlateGray; // 管道颜色
        private readonly Random random = new Random();
        #endregion

        #region 构造函数
        public FunnelControl()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.UserPaint |
                     ControlStyles.DoubleBuffer, true);

            // 初始化动画定时器
            animationTimer = new System.Windows.Forms.Timer { Interval = 30 };
            animationTimer.Tick += AnimationTimer_Tick;
            animationTimer.Start();
        }
        #endregion

        #region 属性
        /// <summary>
        /// 获取或设置水位百分比 (0-100)
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        #endregion

        #region 属性
        /// <summary>
        /// 获取或设置水位百分比 (0-100)
        /// </summary>
        public float WaterLevel
        {
            get => waterLevel;
            set
            {
                waterLevel = Math.Max(0, Math.Min(100, value));
                Invalidate();
            }
        }

        /// <summary>
        /// 获取或设置水流速度
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        /// <summary>
        /// 获取或设置水流速度
        /// </summary>
        public float FlowRate
        {
            get => flowRate;
            set
            {
                flowRate = Math.Max(0.1f, value);
                Invalidate();
            }
        }

        /// <summary>
        /// 获取或设置漏斗颜色
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        /// <summary>
        /// 获取或设置漏斗颜色
        /// </summary>
        public Color FunnelColor
        {
            get => funnelColor;
            set
            {
                funnelColor = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 获取或设置水的颜色
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        /// <summary>
        /// 获取或设置水的颜色
        /// </summary>
        public Color WaterColor
        {
            get => waterColor;
            set
            {
                waterColor = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 获取或设置管道颜色
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        /// <summary>
        /// 获取或设置管道颜色
        /// </summary>
        public Color PipeColor
        {
            get => pipeColor;
            set
            {
                pipeColor = value;
                Invalidate();
            }
        }
        #endregion

        #region 私有方法
        // 动画定时器事件
        private void AnimationTimer_Tick(object sender, EventArgs e)
        {
            animationOffset += flowRate;
            if (animationOffset > 100) animationOffset = 0;
            Invalidate();
        }

        // 绘制漏斗形状
        private void DrawFunnel(Graphics g)
        {
            float centerX = Width / 2f;
            float topWidth = Width * 0.8f;
            float bottomWidth = Width * 0.2f;
            float funnelHeight = Height * 0.6f;
            float pipeHeight = Height * 0.3f;
            float pipeWidth = bottomWidth * 0.7f;

            // 创建漏斗路径
            using (GraphicsPath funnelPath = new GraphicsPath())
            {
                funnelPath.AddLine(centerX - topWidth / 2, 0, centerX - bottomWidth / 2, funnelHeight);
                funnelPath.AddLine(centerX - bottomWidth / 2, funnelHeight, centerX - pipeWidth / 2, funnelHeight);
                funnelPath.AddLine(centerX - pipeWidth / 2, funnelHeight, centerX - pipeWidth / 2, funnelHeight + pipeHeight);
                funnelPath.AddLine(centerX - pipeWidth / 2, funnelHeight + pipeHeight, centerX + pipeWidth / 2, funnelHeight + pipeHeight);
                funnelPath.AddLine(centerX + pipeWidth / 2, funnelHeight + pipeHeight, centerX + pipeWidth / 2, funnelHeight);
                funnelPath.AddLine(centerX + pipeWidth / 2, funnelHeight, centerX + bottomWidth / 2, funnelHeight);
                funnelPath.AddLine(centerX + bottomWidth / 2, funnelHeight, centerX + topWidth / 2, 0);
                funnelPath.CloseFigure();

                // 绘制漏斗
                using (SolidBrush brush = new SolidBrush(funnelColor))
                {
                    g.FillPath(brush, funnelPath);
                }

                // 绘制漏斗边缘（增加立体感）
                using (Pen pen = new Pen(Color.FromArgb(200, Color.Black), 2))
                {
                    g.DrawPath(pen, funnelPath);
                }
            }
        }

        // 绘制漏斗中的水
        private void DrawWater(Graphics g)
        {
            float centerX = Width / 2f;
            float topWidth = Width * 0.8f;
            float bottomWidth = Width * 0.2f;
            float funnelHeight = Height * 0.6f;
            float pipeHeight = Height * 0.3f;
            float pipeWidth = bottomWidth * 0.7f;

            // 计算当前水位对应的高度
            float waterHeight = funnelHeight * waterLevel / 100;

            if (waterHeight <= 0) return;

            // 创建水的路径
            using (GraphicsPath waterPath = new GraphicsPath())
            {
                waterPath.AddLine(centerX - topWidth / 2, funnelHeight - waterHeight, centerX - bottomWidth / 2, funnelHeight);
                waterPath.AddLine(centerX - bottomWidth / 2, funnelHeight, centerX + bottomWidth / 2, funnelHeight);
                waterPath.AddLine(centerX + bottomWidth / 2, funnelHeight, centerX + topWidth / 2, funnelHeight - waterHeight);
                waterPath.CloseFigure();

                // 创建渐变画刷
                using (LinearGradientBrush brush = new LinearGradientBrush(
                    new Point(0, (int)(funnelHeight - waterHeight)),
                    new Point(0, (int)funnelHeight),
                    Color.FromArgb(180, waterColor),
                    Color.FromArgb(255, waterColor)))
                {
                    g.FillPath(brush, waterPath);
                }

                // 绘制水面波纹效果
                DrawWaterRipples(g, centerX, funnelHeight - waterHeight, topWidth);
            }

            // 绘制管道中的水流
            DrawPipeWater(g, centerX, funnelHeight, pipeWidth, pipeHeight);
        }

        // 绘制水面波纹
        private void DrawWaterRipples(Graphics g, float centerX, float waterY, float width)
        {
            using (Pen pen = new Pen(Color.FromArgb(100, waterColor), 2))
            {
                for (int i = 0; i < 3; i++)
                {
                    float offset = (float)Math.Sin((animationOffset + i * 10) * 0.05) * 2;
                    float amplitude = width * 0.05f;
                    float frequency = width * 0.1f;

                    using (GraphicsPath path = new GraphicsPath())
                    {
                        path.StartFigure();
                        for (float x = centerX - width / 2; x <= centerX + width / 2; x += 1)
                        {
                            float y = waterY + offset + (float)Math.Sin((x - centerX) / frequency) * amplitude;
                            path.AddLine(x, y, x + 1, y);
                        }
                        g.DrawPath(pen, path);
                    }
                }
            }
        }

        // 绘制管道中的水流
        private void DrawPipeWater(Graphics g, float centerX, float startY, float pipeWidth, float pipeHeight)
        {
            float flowWidth = pipeWidth * 0.6f;
            float flowStartX = centerX - flowWidth / 2;
            float flowEndY = startY + pipeHeight;

            // 水流主路径
            using (GraphicsPath flowPath = new GraphicsPath())
            {
                flowPath.AddEllipse(flowStartX, startY, flowWidth, flowWidth / 2);
                flowPath.AddRectangle(new RectangleF(flowStartX, startY, flowWidth, flowEndY - startY));

                // 创建水流渐变
                using (LinearGradientBrush brush = new LinearGradientBrush(
                    new PointF(flowStartX, startY),
                    new PointF(flowStartX, flowEndY),
                    Color.FromArgb(200, waterColor),
                    Color.FromArgb(255, waterColor)))
                {
                    g.FillPath(brush, flowPath);
                }
            }

            // 绘制水流中的气泡和波纹
            DrawFlowEffects(g, centerX, startY, flowWidth, pipeHeight);
        }

        // 绘制水流效果（气泡和波纹）
        private void DrawFlowEffects(Graphics g, float centerX, float startY, float flowWidth, float pipeHeight)
        {
            // 绘制流动波纹
            for (int i = 0; i < 5; i++)
            {
                float y = startY + ((animationOffset + i * 20) % pipeHeight);
                float amplitude = flowWidth * 0.1f;

                using (Pen pen = new Pen(Color.FromArgb(100, waterColor), 1))
                {
                    for (float x = centerX - flowWidth / 2; x <= centerX + flowWidth / 2; x += 1)
                    {
                        float offsetY = y + (float)Math.Sin((x - centerX) * 0.5) * amplitude;
                        g.DrawLine(pen, x, offsetY, x, offsetY + 1);
                    }
                }
            }

            // 绘制气泡
            if (random.Next(10) < 3) // 随机生成气泡
            {
                float bubbleSize = random.Next(2, 5);
                float bubbleX = centerX - flowWidth / 2 + random.NextFloat() * flowWidth;
                float bubbleY = startY + (animationOffset + random.Next(0, (int)pipeHeight)) % pipeHeight;

                using (SolidBrush brush = new SolidBrush(Color.FromArgb(100, 255, 255, 255)))
                {
                    g.FillEllipse(brush, bubbleX, bubbleY, bubbleSize, bubbleSize);
                }
            }
        }
        #endregion

        #region 重写方法
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.Clear(BackColor);

            // 绘制漏斗
            DrawFunnel(e.Graphics);

            // 绘制水
            DrawWater(e.Graphics);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Invalidate();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (animationTimer != null)
                {
                    animationTimer.Stop();
                    animationTimer.Tick -= AnimationTimer_Tick;
                    animationTimer.Dispose();
                    animationTimer = null;
                }
            }
            base.Dispose(disposing);
        }
        #endregion
    }

    // 为Random类添加扩展方法
    public static class RandomExtensions
    {
        public static float NextFloat(this Random random)
        {
            return (float)random.NextDouble();
        }
    }
}